<?php
if(!defined('BLOG'))exit('不作死就不会死');
	/**
	 *  @author	Freax
	 *  @2014-3-24
	 *  @contact  huangyanxiong2013@gmail.com
	 */
	class Paging{
		/**
		 * 起始页  
		 * @var int
		 */
		public $startPage;
		/**
		 * 当前页
		 * @var int·
		 */
		public $currenPage;
		/**
		 * 每页显示多少条
		 * @var int
		 */
		public $paging;
		/**
		 * 总页数
		 * @var int
		 */
		public $totalPage;
		/**
		 * 分页连接
		 * @var string
		 */
		public $linkPage;
		/**
		 * 分页中的分类
		 * @var string
		 */
		public $classPage;
		/**
		 * @author Freax
		 * @copyright huangyanxiong
		 * @contact	  huangyanxiong2013@gmail.com
		 * @param int $startPage	起始页  
		 * @param int $currenPage	当前页
		 * @param int $paging		每页显示多少条
		 * @param int $totalPage	总页数
		 * @param string $linkPage	 分页连接
		 * @param string $classPage	分页中的分类
		 */
		public function __construct($paging){
			$this->paging=$paging;
		}
		/**
		 * 获取当前页
		 */
		public function getcurrPage(){
			$this->currenPage=(isset($_GET['page']))?(intval($_GET['page'])):1;
		}			
		/**
		 * 获取起始页
		 */
		public function getStartPage(){
			$this->startPage=$startPage = ($this->currenPage-1)*$this->paging;
			return $this->startPage;
		}
		/**
		 * 获取总页数
		 * @param string $tbale	表名称
		 * @param resouce $db	数据库连接
		 */
		public function getTotalPageAll($db,$tbale){
			$articleTatol=$db->getTableTotal($tbale);
			$this->totalPage=ceil($articleTatol/$this->paging);
		}
		/**
		 * 根据分类获取总页数
		 * @param string $tbale	表名称
		 * @param string $coulumn	列名
		 * @param string $value		分类的值
		 * @param resouce $db	数据库连接
		 */
		public function getTotalPageClass($db,$table,$coulumn,$value,$search=false){
			$articleTatol=$db->getTtableClassTotal($table,$coulumn,$value,$search);
			$this->totalPage=ceil($articleTatol/$this->paging);
			return $this->totalPage;
		}
		/**
		 * 设置分页的连接
		 * @param string $link
		 */
		public function getLinkPage($link){
			$this->linkPage=$link;
		}
		/**
		 * 获取页面连接的分类参数
		 * @param string $class
		 */
		public function getClassPage($class){
			$this->classPage=$class;
		}
		/**
		 * 创建分页连接
		 * @return string
		 */
		public function createPageLinks($typeName='category') {
		if($this->linkPage != ""){
			$this->linkPage .= "?".$typeName."=".$this->classPage."&page=";	
		}else{
			$this->linkPage = "&page=";
		}
	
		$curr_class = "current";/* 定义一个class是 */
		$output = '<div class="page">';  /* 创建一个div */
		if ($this->currenPage != 1) {		/* 当当前页不等于1时 ,也就是不是一条的时候*/
			//$output .= '<a class="first" href="' . $this->linkPage . '1">首页</a>';	/* 创建第一个首页连接 */
			$output .= '<a class="prev" href="' . $this->linkPage . ($this->currenPage - 1) . '">Prev</a>';	/* 创建一个上一页连接 */
		}
	
		if ($this->currenPage < 6 || $this->totalPage < 10) {	/* 当当前页少于6或者少于10时 */
			for($i = 1; $i < 7; $i ++) {
				if ($i != $this->currenPage) {
					$output .= "<a href=\"" . $this->linkPage . $i . "\" class=\"";
					$output .= $this->currenPage == $i ? $curr_class : '';
					$output .= "\">{$i}</a>";
				} else {
					$output .= '<a class="'.$curr_class.'">' . $this->currenPage . '</a>';
				}
				if ($i >= $this->totalPage)
					break;
			}/* 循环十个a连接 */
			if ($this->totalPage > 10)
				$output .= "<a href='" . $this->linkPage . "8'>...</a>";
	
		} elseif ($this->currenPage > 5 && $this->currenPage < $this->totalPage - 4) {
			$output .= "<a href='" . $this->linkPage . ($this->currenPage - 5) . "'>...</a>";
			for($i = $this->currenPage - 4; $i < $this->currenPage + 3; $i ++) {
				$output .= "<a href=\"" . $this->linkPage . $i . "\" class=\"";
				$output .= $this->currenPage == $i ? $curr_class : '';
				$output .= "\">{$i}</a>";
				if ($i >= $this->totalPage)
					break;
			}
			$output .= "<a href=\"" . $this->linkPage . ($this->currenPage + 6) . "\">...</a>";
	
		} elseif ($this->currenPage + 5 > $this->totalPage) {
			$output .= "<a href=\"" . $this->linkPage . ($this->currenPage - 9) . "\">...</a>";
			for($i = $this->totalPage - 8; $i < $this->totalPage+1; $i ++) {
				if ($i > 1) {
					$output .= "<a href=\"" . $this->linkPage . $i . "\" class=\"";
					$output .= $this->currenPage == $i ? $curr_class : '';
					$output .= "\">{$i}</a>";
				}
			}
			if ($this->currenPage < $this->totalPage - 6) {
				$output .= "<a href='" . $this->linkPage . ($this->currenPage + 5) . "'>...</a>";
			}
		}
	
		if ($this->currenPage < $this->totalPage) {
			$output .= '<a class="next" href="' . $this->linkPage . ($this->currenPage + 1) . '">Next</a>';
			//$output .= '<a class="first" href="' . $this->linkPage . $this->totalPage . '">末页</a>';
		}
	
		$output .= '</div>';
	
		//preg_replace_callback('/\?(.*)"/U', 'url_replace', $output);
	
		return $output;
	}
		
	}